<?php
/**
 * Created by JetBrains PhpStorm.
 * User: FangXuanLing
 * Date: 13-11-19
 * Time: 下午6:00
 * To change this template use File | Settings | File Templates.
 */
class PostController extends Controller{


    public function actionIndex(){
        $criteria = new CDbCriteria();
        $criteria->order = 'ctime desc';
        $count = Post::model()->count($criteria);

        $pager = new CPagination($count);
        $pager->pageSize = 5;
        $pager->applyLimit($criteria);

        $posts=Post::model()->with('cate')->findAll(array(
            'limit'=>$pager->pageSize,
            'offset'=>$pager->currentPage*$pager->pageSize,
        ));
        $this->render('index',array('name'=>'文章列表','post'=>$posts,'pages'=>$pager));
        //通过关联表，获取表数据
        //关于with()，可参考：http://www.yiichina.com/guide/database.arr
        //$posts=Post::model()->with('cate')->findAll();
        //var_dump($posts[0]->cate);die;
        //$this->render('index',array('name'=>'文章列表','post'=>$posts));
    }

    public function actionAdd(){
        //是否是post请求
        if(Yii::app()->request->isPostRequest){
            $post = new Post();
            $post->title = Yii::app()->request->getParam('title');
            $post->cid = Yii::app()->request->getParam('cid');
            $post->content = Yii::app()->request->getParam('content');
            $post->tags = Yii::app()->request->getParam('tags');
            $post->status = Yii::app()->request->getParam('status');
            $post->ctime = time();
            $post->save();
            $this->redirect(array('admin/post/index'));
        }
        else{
            //分类列表
            $cate = Category::model()->findAll();
            //传递到模板
            $this->render('add',array('cate'=>$cate));
        }


    }

    public function actionEdit(){
        //获取要编辑的文章id
        $id = Yii::app()->request->getParam('id');
        //判断是否是post请求
        if(Yii::app()->request->isPostRequest){
            //根据获取到文章对象
            $post = Post::model()->findByPk($id);
            //把要更新的数据绑定到对象属性上
            $post->title = Yii::app()->request->getParam('title');
            $post->cid = Yii::app()->request->getParam('cid');
            $post->content = Yii::app()->request->getParam('content');
            $post->tags = Yii::app()->request->getParam('tags');
            $post->status = Yii::app()->request->getParam('status');
            $post->utime = time();
            //更新
            $post->save();
            //跳转
            $this->redirect(array('admin/post/index'));
        }
        else{
            //获取分类
            $cate = Category::model()->findAll();
            //获取文章信息
            $p=Post::model()->find("id=:id",array(":id"=>$id));
            //传递到模板
            $this->render('edit',array('cate'=>$cate,'p'=>$p));
        }
    }

    public function actionDel(){
        //获取要删除的id
        $id = Yii::app()->request->getParam('id');
        //判断id是否存在
        if(empty($id)){
            $this->redirect(array('admin/post/index'));
        }
        else{
            //根据id找到数据
            $model = Post::model()->findByPk($id);
            //软删除
            $model->status = 4;
            //删除
            $model->save();
            //跳转
            $this->redirect(array('admin/post/index'));
            //Yii::app()->user->putSuccessMsg('操作成功啦！');
        }
    }
}